'''
Created on Aug 19, 2012

@author: levilucio
'''
import unittest
import graphs
import graphs_exceptions


class Test(unittest.TestCase):

    def test_is_typed_graph(self):
        """ should be a typed graph """
        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'C', 'd' : 'D'}
        e1 = [('a', 'b', 'containment'),
              ('c', 'd', 'reference')]
        tg = graphs.TG(v1, e1)
#        f = open('/Users/levilucio/desktop/tg.dot', 'w')
#        f.write('digraph myGraph {\n')
#        tg.export_dot(f)
#        f.write('}')
#        f.close()        
    
    def test_all_edge_vertex_defined(self):
        """ should raise an exception if vertices used in E(dges) are not defined in V(ertices) """
        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'A', 'd' : 'D'}
        e1 = [('a', 'b', 'containment'),
              ('e', 'd', 'reference')]
        self.assertRaises(graphs_exceptions.NotAllEdgeVertexDefined, graphs.TG, v1, e1)

    def test_get_next_edges(self):
        """ tests if the next set of vertices is correct """
        v1 = {'a' : 'A', 'b' : 'B', 'b' : 'B', 'a' : 'A', 'b' : 'B', 'c' : 'C'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'a', 'reference'),
              ('b', 'c', 'reference')]
        tg = graphs.TG(v1, e1)
        next_edges = tg.get_next_edges(('a', 'b', 'containment'))
        self.assertSequenceEqual(next_edges, [('b', 'a', 'reference'), ('b', 'c', 'reference')])

    def test_get_previous_edges(self):
        """ tests if the previous set of vertices is correct """
        v1 = {'a' : 'A', 'b' : 'B', 'b' : 'B', 'a' : 'A', 'b' : 'B', 'c' : 'C'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'a', 'reference'),
              ('b', 'c', 'reference')]
        tg = graphs.TG(v1, e1)
        next_edges = tg.get_next_edges(('b', 'a', 'reference'))
        self.assertSequenceEqual(next_edges, [('a', 'b', 'containment')])
        
    def test_get_edge_starting_with_vertex(self):
        """ tests if the next set of vertices is correct """
        v1 = {'a' : 'A', 'b' : 'B', 'b' : 'B', 'a' : 'A', 'b' : 'B', 'c' : 'C'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'a', 'reference'),
              ('b', 'c', 'reference')]
        tg = graphs.TG(v1, e1)
        edge = tg.get_edge_starting_with_vertex('a')
        self.assertEqual(edge, ('a', 'b', 'containment'))

    def test_get_edge_ending_with_vertex(self):
        """ tests if the next set of vertices is correct """
        v1 = {'a' : 'A', 'b' : 'B', 'b' : 'B', 'a' : 'A', 'b' : 'B', 'c' : 'C'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'a', 'reference'),
              ('b', 'c', 'reference')]
        tg = graphs.TG(v1, e1)
        edge = tg.get_edge_ending_with_vertex('a')
        self.assertEqual(edge, ('b', 'a', 'reference'))         
                
#    def test_indirect_edges_in_typedgraph_edges(self):
#        """ should raise an exception if the indirect edges are not defined within the graph's edges """
#        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'A', 'd' : 'D'}
#        e1 = [('a', 'b', 'containment'),
#              ('c', 'd', 'reference')]
#        il1 = [('e', 'f', 'containment')]
#        self.assertRaises(myExceptions.NotIndirectEdgesInTypedGraphEdges, graphs.ITG, v1, e1, il1)   

    def test_is_indirect_graph(self):
        """ should raise an exception if the indirect edges are not possible within the graph's edges """
        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'A', 'd' : 'D'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'd', 'reference'),
              ('b', 'c', 'containment')]
        il1 = [('a', 'c')]
        itg = graphs.ITG(v1, e1, il1)
        f = open('/Users/levilucio/desktop/itg.dot', 'w')
        f.write('digraph myGraph {\n')
        itg.export_dot(f)
        f.write('}')
        f.close()              

    def test_indirect_edges_possible(self):
        """ should raise an exception if the indirect edges are not possible within the graph's edges """
        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'A', 'd' : 'D'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'd', 'reference'),
              ('b', 'c', 'containment')]
        il1 = [('a', 'c'), ('a', 'b')]
        itg = graphs.ITG(v1, e1, il1)
        self.assertIsInstance(itg, graphs.ITG, '')   
                        
    
    def test_indirect_edges_impossible(self):
        """ should raise an exception if the indirect edges are not possible within the graph's edges """
        v1 = {'a' : 'A', 'b' : 'B', 'c' : 'A', 'd' : 'D'}
        e1 = [('a', 'b', 'containment'),
              ('b', 'd', 'reference'),
              ('b', 'c', 'containment')]
        il1 = [('a', 'c'), ('a', 'd')]
        self.assertRaises(graphs_exceptions.ImpossibleIndirectEdges, graphs.ITG, v1, e1, il1)                         
        

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
